package sajat.onlab;

import sajat.onlab.AsyncTaskGetRoute.GetRouteCompleteListener;
import sajat.onlab.AsyncTaskRGeocode.RGeocodeCompleteListener;
import sandbox.keki.aut.bme.hu.CommentPoiActivity;
import sandbox.keki.aut.bme.hu.EditPoiAsyncTask;
import sandbox.keki.aut.bme.hu.GetPoiCommentsAsyncTask;
import sandbox.keki.aut.bme.hu.EditPoiAsyncTask.Action;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;

public class PoiDetailsActivity extends Activity implements GetRouteCompleteListener, RGeocodeCompleteListener{
	private Button rate;
	private Button gothere;
	private Button rate_dialog;
	private Button comment;//DEMO
	private TextView poiname;
	private TextView poipos;
	private TextView desc;
	private ScrollView scrolldesc;
	private RatingBar poirate;
	private GeoPoint pos;
	private Poi poiinstance;
	private TextView commentsTV;
	private EditText myCommentET;
	
	private SharedPreferences sp;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		sp = this.getSharedPreferences("logininfo", this.MODE_PRIVATE);
		
		poiinstance = getIntent().getParcelableExtra("poiinstance");
		
		int[] temppos = getIntent().getIntArrayExtra("position");
		pos = new GeoPoint (temppos[0],temppos[1]);
		
		setContentView(R.layout.poidetails);

		poiname = (TextView) findViewById(R.id.poiname);
		poipos = (TextView) findViewById(R.id.poipos);
		desc = (TextView) findViewById(R.id.poidesc);
		scrolldesc = (ScrollView) findViewById(R.id.scrolldesc);
		rate = (Button) findViewById(R.id.poiratebutton);
		gothere = (Button) findViewById(R.id.gotherebutton);
		comment = (Button) findViewById(R.id.poicommentbutton);
		commentsTV = (TextView) findViewById(R.id.commentstext);
		myCommentET = (EditText) findViewById(R.id.poicomment);
		
		rate.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				final Dialog dialog = new Dialog(v.getContext());
                dialog.setContentView(R.layout.ratelayout);
                dialog.setTitle("Rate this poi...");
                dialog.setCancelable(true);
                poirate = (RatingBar) dialog.findViewById(R.id.ratingBar1);
                rate_dialog = (Button) dialog.findViewById(R.id.ratebutton2);
                rate_dialog.setOnClickListener(new OnClickListener() {
                	@Override
                	public void onClick(View v) {
                		EditPoiAsyncTask epoiAT = new EditPoiAsyncTask(v.getContext(), sp.getString("username", ""), poiinstance.getID(), Action.RATE);
        				epoiAT.execute(Float.toString(poirate.getRating()));
        				dialog.cancel();
                	}
                });
                dialog.show();
			}
		});
		
		gothere.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				GetRoute("	http://maps.googleapis.com/maps/api/directions/xml?origin="+
						(pos.getLatitudeE6()/1E6)+","+(pos.getLongitudeE6()/1E6)
						+"&destination="+poiinstance.getPlace().getLatitudeE6()/1E6
						+","+poiinstance.getPlace().getLongitudeE6()/1E6
						+"&sensor=false");
			}
		});
		
		comment.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				EditPoiAsyncTask editPoiAT = new EditPoiAsyncTask(v.getContext(), sp.getString("username", ""), poiinstance.getID(), Action.COMMENT);	//context,userID, poiID, action0
				editPoiAT.execute(myCommentET.getText().toString());
			}
		});
		
		poiname.setText(poiinstance.getName());
		poipos.setText(poiinstance.getPlace().toString());
		desc.setText(poiinstance.getDescription());
		
		GetPoiCommentsAsyncTask gpcAT = new GetPoiCommentsAsyncTask(commentsTV, sp.getString("username", ""), poiinstance.getID());
		gpcAT.execute();
		
		GetAddress("http://maps.googleapis.com/maps/api/geocode/xml?latlng="
				+(poiinstance.getPlace().getLatitudeE6()/1E6)+","+(poiinstance.getPlace().getLongitudeE6()/1E6)
				+"&sensor=false");
	}

	@Override
	public void onTaskComplete(String[] aText) {
		RouteStep step;
		GeoPoint start;
		GeoPoint end;
		RouteSingleton.getInstance().deleteSteps();
		//while (mytokenizer.hasMoreTokens()) {
		for(int i=0; i<aText.length; i+=8) {
			start = new GeoPoint(
					(int)(Double.parseDouble(/*mytokenizer.nextToken()*/aText[i])*1E6), //latitude
					(int)(Double.parseDouble(/*mytokenizer.nextToken()*/aText[i+1])*1E6)  //longitude
					);
			end = new GeoPoint(
					(int)(Double.parseDouble(/*mytokenizer.nextToken()*/aText[i+2])*1E6), //latitude
					(int)(Double.parseDouble(/*mytokenizer.nextToken()*/aText[i+3])*1E6)	//longitude
					);
			step=new RouteStep(
					start,											//startpoint
					end,											//endpoint
					/*mytokenizer.nextToken()*/aText[i+4],						//polyline
					Double.parseDouble(/*mytokenizer.nextToken()*/aText[i+5]),	//duration
					/*mytokenizer.nextToken()*/aText[i+6],						//instructions
					Double.parseDouble(/*mytokenizer.nextToken()*/aText[i+7])		//distance
					);
			RouteSingleton.getInstance().add(step);
		}
		
		Intent myIntent = new Intent();
		int[] temp = new int[2];
		temp[0] = pos.getLatitudeE6();
		temp[1] = pos.getLongitudeE6();
		myIntent.putExtra("sajat.onlab.coordinates", temp);
		myIntent.setClassName("sajat.onlab",
		    "sajat.onlab.MyMapActivity");
		startActivity(myIntent);
		finish();
		
	}

	@Override
	public void onError(String aError) {
		Toast.makeText(this,"Network error: "+" "+aError, Toast.LENGTH_LONG).show();
		
	}
	
	public void GetRoute(String URL) {
		AsyncTaskGetRoute getRouteTask = 
            new AsyncTaskGetRoute(this, this, "Please wait...");
        getRouteTask.execute(URL);
	}

	@Override
	public void onTaskCompleteRG(String[] aText) {
		poipos.setText(aText[0]);
		
	}

	@Override
	public void onErrorRG(String aError) {
		// TODO Auto-generated method stub
		
	}
	
	public void GetAddress(String URL) {
		AsyncTaskRGeocode getRGeoTask = 
            new AsyncTaskRGeocode(this, this);
        getRGeoTask.execute(URL);
	}
}
